package com.eazy.lkv2.web.controller.traffic;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.City;
import com.eazy.lkv2.web.service.traffic.CityService;

@Controller
@RequestMapping("/city")
public class CityController extends BaseController {

	@Autowired
	private CityService cityService;

	/**
	 * 城市查询
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("data", cityService.select());
		return "traffic/city_list";
	}

	/**
	 * 城市添加功能
	 */
	@RequestMapping(value = "addcity", method = RequestMethod.POST)
	public void addCity(City city) {
		city.setStatus("0");
		renderResult(cityService.create(city) > 0);
	}

	/**
	 * 城市修改功能
	 */
	@RequestMapping(value = "updatecity", method = RequestMethod.POST)
	public void updCity(City city) {
		renderResult(cityService.update(city) > 0);
	}

	/**
	 * 城市删除
	 */
	@RequiresPermissions("sys:city:delete")
	@RequestMapping(value = "deletecity/{id}", method = RequestMethod.GET)
	public String delCity(@PathVariable String id) {
		City entity = cityService.findById(Integer.valueOf(id));
		switch (entity.getStatus()) {
		case "0":
			entity.setStatus("1");
			cityService.update(entity);
			break;
		case "1":
			entity.setStatus("0");
			cityService.update(entity);
			break;
		}
		return "redirect:/city/list";
	}

	/**
	 * 跳转到城市添加页面
	 */
	@RequestMapping(value = "toAddcity", method = RequestMethod.GET)
	public String toAddcity() {
		return "traffic/city_add";
	}

	/**
	 * 跳转到城市修改页面
	 */
	@RequestMapping(value = "toUpdatecity/{id}", method = RequestMethod.GET)
	public ModelAndView toUpdatecity(@PathVariable String id) {
		return getSuccess("traffic/city_update", cityService.findById(parseInt(id)));
	}
}
